import { Observable, Subscriber } from 'rxjs'
import { createSocket, SocketType, RemoteInfo, Socket } from 'dgram'

export function createDgramSocket(
  type: SocketType = 'udp4'
): Observable<{ server: Socket; msg: Buffer; rinfo: RemoteInfo }> {
  return Observable.create(
    (obser: Subscriber<{ server: Socket; msg: Buffer; rinfo: RemoteInfo }>) => {
      const server = createSocket(type, (msg: Buffer, rinfo: RemoteInfo) => {
        obser.next({
          server,
          msg,
          rinfo
        })
      })
    }
  )
}
